<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= puVarIns();
	$process_unit = sxREQUEST("process_unit");
	$alias = sxREQUEST("alias");
	$variable = sxREQUEST("variable");
	$description = sxREQUEST("description");
	$read_only = (isset($_REQUEST["read_only"]))?"Y":"N";
	$note = sxREQUEST("note");
}else{
	$process_unit = sxREQ("pu");
	$alias = sxREQ("alias");
	$variable = "";
	$description = "";
	$read_only = "";
	$note = "";
}
if($process_unit == ""){
	$errMsg .= _t("Il processo non &egrave; definito<br>");
}
if($alias == ""){
	$errMsg .= _t("L'alias non &egrave; definito<br>");
}
//output HTML
$aOpz = array("tit2"=>_t("Crereqtype nuova variabile"), "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script>  
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	var aElemObbl = Array("alias", "variable");
	var aDescrElemObbl = Array("alias", "variabile");	
</script>  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"puVarIns.php\" method=\"POST\" enctype=\"multipart/form-data\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"process_unit\" id=\"process_unit\" value=\"$process_unit\">");
echo("<input type=\"hidden\" name=\"alias\" id=\"alias\" value=\"$alias\">");
//output comandi
$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati della nuova variabile"),
										"text"=>_t("salva")
										),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1)
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Variabile")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("processo")." (*)</td>" .
			"<td class=\"text01\">$process_unit</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("alias")." (*)</td>" .
			"<td class=\"text01\">$alias</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("variabile")." (*)</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"variable\" id=\"variable\" maxlength=\"80\" size=\"80\" value=\"".sxHE($variable)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("description")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
$chk = "";
if($read_only == "Y")
	$chk = "CHECKED";
echo("<tr>" .
			"<td class=\"labl01\">"._t("sola lettura")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"checkbox\" name=\"read_only\" id=\"read_only\" $chk></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("note")."</td>" .
			"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
echo("</table>");
echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function puVarIns($redirOnEnd = true){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";
	if(!isset($_REQUEST["process_unit"]) || $_REQUEST["process_unit"] == "")
		$errMsg .= _t("Il processo &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["variable"]) || $_REQUEST["variable"] == "")
		$errMsg .= _t("La variabile &egrave; obbligatoria").".<br>";
	if(isset($_REQUEST["variable"]) && ereg("[^a-zA-Z 0-9 - _]",$_REQUEST["variable"]))
		$errMsg .= _t("La variable deve essere un alfanumerico").".<br>";
	if(!isset($_REQUEST["alias"]) || $_REQUEST["alias"] == "")
		$errMsg .= _t("L'alias &egrave; obbligatorio").".<br>";
	if(isset($_REQUEST["alias"]) && ereg("[^a-zA-Z 0-9 - _]",$_REQUEST["alias"]))
		$errMsg .= _t("L'alias deve essere un alfanumerico").".<br>";
	
	
	//controllo che il link non esista gi@
	$sSQL = "SELECT count(*) as num FROM pu_variable " .
			"WHERE process_unit='".$_REQUEST["process_unit"]."' " .
			"AND variable='".$_REQUEST["variable"]."'" . 
			"AND alias='".$_REQUEST["alias"]."'";
	$num = 0;
	if($objSDAO->query($sSQL,"test",array("contOnErr"=>true))){
		if($row = $objSDAO->fetch("test")){
			$num = $row["num"]; 
		}
	}else{
		$errMsg .= $objSDAO->error;
	}
	if($num != 0)
		$errMsg .= _t("La variabile")." '".$_REQUEST["variable"]."' "._t("esiste gi&agrave;.")."<br>";
	if($errMsg == ""){		
		$read_only = (isset($_REQUEST["read_only"]))?"Y":"N";
		$sSQL = "INSERT INTO " .
				"pu_variable " .
				"(process_unit, " .
				"alias, " .
				"variable, " .
				"description, " .
				"read_only, " .
				"note)" .
				"VALUES('".addslashes(sxGPC($_REQUEST["process_unit"]))."', " .
				"'".strtoupper(addslashes(sxGPC(trim($_REQUEST["alias"]))))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["variable"])))."', " .
				"'".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
				"'$read_only', " .
				"'".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
				")";	
		
		if($objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			$process_unit = addslashes(sxGPC($_REQUEST["process_unit"]));
			$alias = addslashes(sxGPC($_REQUEST["alias"]));
			$_SESSION[USEROBJ]->reLoad();
			logRecord("300", _t("Inserito nuova variabile nel processo")." '".$process_unit."'");
			//serverRedir("puVarList.php?pu=$process_unit&alias=$alias&xsn=".XSN);
			if($redirOnEnd){
				$url = $_SESSION[USEROBJ]->getHistory(0, true);
				serverRedir($url);
			}
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
}
